package Radar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import syncmaster.BeatModelInterface;
import syncmaster.RadarModel;

public class Flash extends Thread{ 
	   private DrawA dA; 
	   private int x; 
	   private int xCor[]; 
	   private int yCor[]; 
	   private int i; 
	   private int dir; 
	   private BufferedImage im; 
	   private Graphics buff; 
	   public int BPM;
	   public BeatModelInterface _model;
	   
	   public Flash(DrawA dA, BufferedImage im, Graphics buff, BeatModelInterface model){ 
	      this.dA = dA; 
	      this.im = im; 
	      this.buff = buff; 
	      xCor = new int[451]; 
	      yCor = new int[451]; 
	      dir = 5;
	      _model = model;
	   }

	public void run(){ 
	      
	      while(true) {
	      
	      
	      for(x = 75, i = 0; x <= 525; x++, i++){ 
	         xCor[i] = x - 300; 
	      } 
	      for(i = 0; i <= 450; i++){ 
	         yCor[i] = (int)Math.sqrt(62500 - (xCor[i] * xCor[i])); 
	         yCor[i] = 300 - yCor[i]; 
	      }

	      for(x = 0; x <= 450; x++){ 
	         xCor[x]+=300; 
	      }

	      i = 0; 
	      while(true){ 
	         try{ 
	            Thread.sleep(700/_model.getBPM());
	         } 
	         catch(Exception e){} 
	 

	         dA.repaint(); 
	         i += dir;

	         if(i < 0 || i > 450){ 
	           dir = -dir; 
	            i += dir; 
	         }
	         
	      } 
	   }
	}
	   public void draw(Graphics g){ 
	      buff.setColor(Color.black); 
	      buff.fillRect(0, 0, 600, 330); 
	      buff.setColor(new Color(0,40,0)); 
	      buff.fillArc(50, 50, 500, 500, 25, 130); 
	      buff.setColor(Color.green); 
	      if(i > 450) 
	         return; 
	      if(xCor[i] != 0 && yCor[i] != 0) 
	         buff.drawLine(xCor[i], yCor[i], 300, 300); 
	      
	      if(dir > 0){ 
	         for(int j = i; j > (i - 10); j--){ 
	            if(j >= 0 && j <= 450){  
	               buff.setColor(Color.green); 
	               buff.drawLine(xCor[j], yCor[j], 300, 300);
	               buff.drawLine(xCor[j], yCor[j], 300, 300);
	            } 
	         } 
	      } 
	      else{ 
	         for(int j = i; j < (i + 10); j++){ 
	            if(j >= 0 && j <= 450){  
	               buff.setColor(Color.green); 
	               buff.drawLine(xCor[j], yCor[j], 300, 300);
	               buff.drawLine(xCor[j], yCor[j], 300, 300);
	            }
	         } 
	      } 
	      
	      buff.setColor(Color.green); 
	      buff.drawArc(112, 112, 375, 375, 25, 130);
	      buff.drawArc(174, 174, 250, 250, 25, 130);
	      buff.drawArc(238, 238, 125, 125, 25, 130);
	      
	      g.drawImage(im, 0, 0, null); 
	   } 
	} 